package com.luna.servlets;

import com.luna.beans.User;
import com.luna.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(
    name = "RoleShunt",
    urlPatterns = "/RoleShunt"
)
public class RoleShunt extends HttpServlet {

    public RoleShunt() {}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html charset=UTF-8");
        User user = (User) req.getSession().getAttribute("user");
        if (user.getState() == 1) { // 管理员
            req.getRequestDispatcher("admin-index.jsp").forward(req, resp);
        } else if (user.getState() == 0) { // 普通用户
            req.getRequestDispatcher("/").forward(req, resp);
        } else { // 封禁用户
            req.getRequestDispatcher("/restricted-login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
